package cn.shuto.liferay.plugin.chart.portlet.template;


import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import cn.shuto.liferay.plugin.chart.service.model.ChartTemplate;
import cn.shuto.liferay.plugin.chart.service.model.Option;
import cn.shuto.liferay.plugin.chart.service.service.ChartTemplateLocalServiceUtil;
import cn.shuto.liferay.plugin.chart.service.util.JsonUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class ChartTemplatePortlet
 */
public class ChartTemplatePortlet extends MVCPortlet {
	/*
	 * 添加或修改一个模板
	 */
	public void addOrUpdateChartTemplate(ActionRequest request, ActionResponse response)throws PortalException, SystemException, IOException {
		long chartTemplateId = ParamUtil.getLong(request, "chartTemplateId");
		String cmd = ParamUtil.getString(request, Constants.CMD);
		if (Validator.isNotNull(cmd)) {
			ServiceContext serviceContext = ServiceContextFactory.getInstance(
					Option.class.getName(), request);
			String name = ParamUtil.getString(request, "name");
			String description = ParamUtil.getString(request, "description");
			String script = ParamUtil.getString(request, "script");
			String templateParam = JsonUtil.getTemplateParam(request);
			
			@SuppressWarnings("unused")
			ChartTemplate template = null;
			if (cmd.equals(Constants.ADD) ) {
				template=ChartTemplateLocalServiceUtil.addTemplate(name, description, script,templateParam, serviceContext);
			}
			else if (cmd.equals(Constants.UPDATE)) {
				template=ChartTemplateLocalServiceUtil.updateTemplate(chartTemplateId, name, description, script, templateParam,serviceContext);
			}
		}
	}
	
	/*
	 * 删除一个模板
	 */
	public void deleteChartTemplate(ActionRequest request, ActionResponse response)throws PortalException, SystemException {
		long chartTemplateId = ParamUtil.getLong(request, "chartTemplateId");
		ChartTemplateLocalServiceUtil.deleteChartTemplate(chartTemplateId);
	}
}
